
import groovy.grape.Grape
Grape.grab(group:"org.apache.camel", module:"camel-core", version:"2.0.0")
Grape.grab(group:"org.apache.camel", module:"camel-jetty", version:"2.0.0")
Grape.grab(group:"org.apache.camel", module:"camel-freemarker", version:"2.0.0")

class QuoteServiceBean {
  public String usStock(String symbol) {
    "${symbol}: 123.50 US\$"
  }
  public String hkStock(String symbol) {
    "${symbol}: 90.55 HK\$"
  }
}

class MyRouteBuilder extends org.apache.camel.builder.RouteBuilder { 
  void configure() { 
    from("direct://quote").choice()
      .when(body().contains(".HK")).bean(QuoteServiceBean.class, "hkStock")
      .otherwise().bean(QuoteServiceBean.class, "usStock")
      .end().to("mock://result")
    from("direct://xmlquote").transform().xpath("//quote/@symbol", String.class).to("direct://quote")
	//curl -H "Content-Type: text/xml" http://localhost:8080/quote?symbol=IBM
    from('jetty:http://localhost:8080/quote').transform()
       .simple('<quote symbol="${header.symbol}"></quote>').to("direct://xmlquote").choice()
           .when(header("Content-Type").isEqualTo("text/xml")).to("freemarker:xmlquote.ftl")
           .otherwise().to("freemarker:htmlquote.ftl")
         .end()
  }
}

ctx = new org.apache.camel.impl.DefaultCamelContext()
mrb = new MyRouteBuilder()
ctx.addRoutes mrb
ctx.start()
p = ctx.createProducerTemplate()

p.sendBody("direct:quote", "00005.HK")
p.sendBody("direct:xmlquote", "<quote symbol='IBM'/>")
p.sendBody("direct:xmlquote", "<quote symbol='00005.HK'/>")

e = ctx.getEndpoint("mock://result")
e.exchanges.each { ex ->
  println "INFO> in.body='${ex.in.body}'"
}
